package com.hue.ps.topic;

import com.hue.utils.ConnectionUtils;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/*
    发布与订阅模式之topic：生产者
 */
public class Product {
    public static void main(String[] args) throws IOException, TimeoutException {
        //2.获取连接
        Connection connection = ConnectionUtils.getConnection();
        //3.获取频道
        Channel channel = connection.createChannel();

        //4.创建交换机,参数1：交换机名，参数2：交换机类型
        channel.exchangeDeclare("topic_exchange", BuiltinExchangeType.TOPIC);

        //5.声明两个队列
        channel.queueDeclare("topic_queue1", true, false, false, null);
        channel.queueDeclare("topic_queue2", true, false, false, null);

        //6.将某个队列绑定到某个交换机上，并指定routerKey
        channel.queueBind("topic_queue1","topic_exchange", "user.#");
        channel.queueBind("topic_queue2","topic_exchange", "user.*");

        //5.发送消息,并指定消息的routerKey
        channel.basicPublish("topic_exchange", "user.update", null, "msg key = user.update".getBytes());
        channel.basicPublish("topic_exchange", "user.admin.insert", null, "msg key = user.admin.insert".getBytes());

        //6.关闭资源
        channel.close();
        connection.close();
    }
}
